/**
 * 为什么j--要在i++前
 */
public class QuickSort {
    static void quickSort(int[] arr,int left,int right){
        if(right-left<=1){
            return;
        }
        int div=partition(arr,left, right);
        quickSort(arr,left,div);
        quickSort(arr,div+1,right);
    }
    //士兵法
    public static int partition1(int[] arr,int left,int right){
        int i=left,j=right,k=arr[left];
        while(i<j){
            while(i<j&&arr[j]>=k){
                j--;
            }
            while(i<j&&arr[i]<=k){
                i++;
            }

            swap(arr,i, j);
        }
        swap(arr,i,left);
        return i;
    }
    //挖坑法
    public static int partition(int[] arr,int left,int right){
        int k=arr[left];
        int i = left;
        int j = right;
        while (i<j){
            while(i<j&&arr[j]>=k){
                j--;
            }
            arr[i]=arr[j];
            while(i<j&&arr[i]<=k){
                i++;
            }
            arr[j]=arr[i];
        }
        arr[i]=k;
        return i;

    }
    private static int partition3(int[] array, int left, int right) {
        int i = left;
        int j = right;
        int pivot = array[left];
        while (i < j) {
            while (i < j && array[j] >= pivot) {
                j--;
            }
            array[i] = array[j];
            while (i < j && array[i] <= pivot) {
                i++;
            }
            array[j] = array[i];
        }
        array[i] = pivot;
        return i;
    }



    public static void swap(int[] arr, int left, int right) {
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
    }
    public static void main(String[] args) {
        int[] arr={9,8,7,6,5,4,10,3,2,1};
        quickSort(arr,0, arr.length-1);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}
